package com.quectel.cms.controller.household;


import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.DesensitizedUtil;
import com.quectel.cms.controller.BaseController;
import com.quectel.constant.global.SystemConstants;
import com.quectel.constant.global.annotation.RequirePermissions;
import com.quectel.core.module.household.dto.HouseholdDto;
import com.quectel.core.module.household.dto.HouseholdRoomDto;
import com.quectel.core.module.household.service.HouseholdRoomService;
import com.quectel.core.module.village.dto.VillageRoomDto;
import com.quectel.core.module.village.service.VillageRoomService;
import com.quectel.util.common.Page;
import com.quectel.util.common.Request;
import com.quectel.util.common.Response;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author caolu
 * @email louis.cao@quectel.com
 * @date 2021-11-02 13:12:15
 */
@RestController
@RequestMapping("householdRoom")
@Tag(name = "HouseholdRoomController", description = "住户房间相关api")
public class HouseholdRoomController extends BaseController {

    @DubboReference
    private HouseholdRoomService householdRoomService;
    @DubboReference
    private VillageRoomService villageRoomService;


    /**
     * 列表
     */
    @GetMapping("queryAllBy")
    @Operation(summary = "根据条件查询住户房间")
    public Response<List<HouseholdRoomDto>> queryAllBy(
            @Parameter(description = "房间 id") @RequestParam(required = false) Long roomId,
            @Parameter(description = "住户 id") @RequestParam(required = false) Long householdId
    ) {
        Map<String, Object> params =
                Request.parseParams(httpServletRequest);
        List<HouseholdRoomDto> householdRoomDtos = householdRoomService.queryList(params);
        if (CollectionUtil.isNotEmpty(householdRoomDtos)) {
            for (HouseholdRoomDto householdRoomDto : householdRoomDtos) {
                HouseholdDto householdDto = householdRoomDto.getHouseholdDto();
                if (householdDto != null) {
                    householdDto.setName(DesensitizedUtil.chineseName(householdDto.getName()));
                    householdDto.setMobile(DesensitizedUtil.mobilePhone(householdDto.getMobile()));
                    householdDto.setCardNo(DesensitizedUtil.idCardNum(householdDto.getCardNo(), 2, 2));
                }
            }
        }
        return Response.<List<HouseholdRoomDto>>ok().wrap(householdRoomDtos);
    }

    /**
     * 列表
     */
    @GetMapping("list")
    @RequirePermissions(values = "householdRoom:list")
    @Operation(summary = "住户房间查询分页列表")
    public Response<Page<HouseholdRoomDto>> list(
            @Parameter(description = "从1开始的页码数") @RequestParam Integer pageNo,
            @Parameter(description = "每页多少条") @RequestParam Integer pageSize
    ) {
        //查询列表数据
        Request request = Request.configParams(Request.parseParams(httpServletRequest));

        super.fillAuthorityData(request.getParams());
        int total = householdRoomService.queryTotal(request.getParams());

        List<HouseholdRoomDto> list = null;
        if (total > 0) {
            list = householdRoomService.queryList(request.getParams());
        } else {
            list = new ArrayList<>();
        }

        Page<HouseholdRoomDto> page = new Page<>(request.getPageNo(), request.getPageSize(), total, list);
        return Response.<Page<HouseholdRoomDto>>ok().wrap(page);
    }


    /**
     * 信息
     */
    @GetMapping("info/{id}")
    @RequirePermissions(values = "householdRoom:info")
    @Operation(summary = "查询住户房间单条记录")
    public Response<HouseholdRoomDto> info(@PathVariable("id") Long id) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));
        if (householdRoomService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }

        HouseholdRoomDto householdRoomDto = householdRoomService.selectById(id);

        return Response.<HouseholdRoomDto>ok().wrap(householdRoomDto);
    }

    /**
     * 保存
     */
    @PostMapping("save")
    @RequirePermissions(values = "householdRoom:edit")
    @Operation(summary = "保存住户房间单条记录")
    public Response<Object> save(@RequestBody @Validated HouseholdRoomDto householdRoomDto) {

        //填充基础数据
        fillSaveCommonData(householdRoomDto);
        VillageRoomDto villageRoomDto = villageRoomService.selectCacheById(householdRoomDto.getRoomId());
        if (villageRoomDto == null) {
            return Response.error("房间不存在");
        }
        householdRoomDto.setVillageId(villageRoomDto.getVillageId());

        householdRoomService.save(householdRoomDto);

        return Response.ok();
    }

    /**
     * 修改
     */
    @PostMapping("update")
    @RequirePermissions(values = "householdRoom:edit")
    @Operation(summary = "修改住户房间单条记录")
    public Response<Object> update(@RequestBody @Validated HouseholdRoomDto householdRoomDto) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);
        params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(householdRoomDto.getId()));
        if (householdRoomService.queryTotal(params) <= 0) {
            return Response.error(SystemConstants.ILLEGAL_MSG);
        }
        //填充基础数据
        fillEditCommonData(householdRoomDto);

        householdRoomService.updateById(householdRoomDto);

        return Response.ok();
    }

    /**
     * 删除
     */
    @PostMapping("delete")
    @RequirePermissions(values = "householdRoom:delete")
    @Operation(summary = "批量删除住户房间记录")
    public Response<Object> delete(@RequestBody Long[] ids) {

        Map<String, Object> params = new HashMap<>();
        super.fillAuthorityData(params);

        for (Long id : ids) {

            params.remove(SystemConstants.ENTITY_ID_FLAG);
            params.put(SystemConstants.ENTITY_ID_FLAG, String.valueOf(id));

            if (householdRoomService.queryTotal(params) <= 0) {
                return Response.error(SystemConstants.ILLEGAL_MSG);
            }
        }

        householdRoomService.deleteBatch(ids);

        return Response.ok();
    }
}
